import { defineStore } from 'pinia'
import { ref } from 'vue'
import { loginApi } from '@/service/auth'
import config from '@/utils/config'
import { getInfo } from '@/service/system'
import * as wsApi from '@/utils/wssocket'

const initState = {
  token: '',
  ClientId: config.clientID,
  tenantId: config.tenantId,
  avatar: '',
  roles: [],
  permissions: '',
  userId: '',
  userName: '',
  userType: '',
  nickName: '',
  phonenumber: '',
  sex: '',
  status: '',
  deptId: '',
  email: '',
  schoolName: '',
  roleList: [],
  headshot: '',
}

export const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref<IUserInfo>({ ...initState })

    const setUserInfo = (val: IUserInfo) => {
      userInfo.value = val
    }

    const clearUserInfo = () => {
      wsApi.closeSocket(3099)
      userInfo.value = { ...initState }
    }

    const isLogined = computed(() => !!userInfo.value.token)

    const handleLogin = async (val: loginInfo) => {
      val.clientId = initState.ClientId
      val.tenantId = initState.tenantId
      return new Promise((resolve, reject) => {
        loginApi(val)
          .then((res: any) => {
            console.log('res=========', res)
            if (res.code === 200) {
              userInfo.value.ClientId = res?.data?.client_id
              userInfo.value.token = res?.data?.access_token
              resolve({})
            } else {
              uni.showToast({
                title: res?.msg,
                icon: 'none',
              })
              reject(res)
            }
          })
          .catch((error) => {
            uni.showToast({
              title: '网络异常，请稍后再试',
              icon: 'none',
            })
            reject(error)
            console.log('error=========', error)
          })
      })
    }
    const getUserInfo = async () => {
      return new Promise((resolve, reject) => {
        getInfo()
          .then((res: any) => {
            console.log('res=========', res)
            if (res.code === 200) {
              userInfo.value.userId = res?.data?.user?.userId
              userInfo.value.userType = res?.data?.user?.userType
              userInfo.value.userName = res?.data?.user?.userName
              userInfo.value.tenantId = res?.data?.user?.tenantId
              userInfo.value.sex = res?.data?.user?.sex
              userInfo.value.status = res?.data?.user?.status
              userInfo.value.avatar = res?.data?.user?.avatar
              userInfo.value.email = res?.data?.user?.email
              userInfo.value.nickName = res?.data?.user?.nickName
              userInfo.value.phonenumber = res?.data?.user?.phonenumber
              userInfo.value.roles = res?.data?.user?.roles
              userInfo.value.schoolName = res?.data?.user?.schoolName
              userInfo.value.roleList = res?.data?.roles
              userInfo.value.headshot = res?.data?.user?.headshot
              resolve({})
            }
          })
          .catch((error) => {
            console.log('error=========', error)
          })
      })
    }

    return {
      userInfo,
      setUserInfo,
      clearUserInfo,
      isLogined,
      handleLogin,
      getUserInfo,
    }
  },
  {
    persist: true,
  },
)
